//
//  EBBaseNavController.swift
//  ProductBase
//
//  Created by 许必成 on 2020/7/23.
//  Copyright © 2020 东信北邮信息技术有限公司 All rights reserved.
//

import UIKit

class EBBaseNavController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.interactivePopGestureRecognizer?.isEnabled = false
    }
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        
        if self.children.count == 1 {
            return false
        }
        
        let pan = gestureRecognizer as! UIPanGestureRecognizer
        
        return self.commiteTransition(transition: pan.translation(in: self.view))
        
    }
    
    /**
    *   判断手势方向
    *
    *  @param translation translation description
    */
    func commiteTransition(transition: CGPoint) -> Bool{
        
        
        let absX = abs(transition.x)
        let absY = abs(transition.y)

        if (absX > absY ) {
            
            if (transition.x<0) {

                return false;
                //向左滑动
            }else{

                return true;
                //向右滑动
            }
            
        } else if (absY > absX) {
            if (transition.y<0) {

                return false;
                //向上滑动
            }else{

                return false;
                //向下滑动
            }
        }
        return false;
        
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if self.children.count > 0 {
            
            viewController.navigationItem.addBackItem(target: self, action: #selector(back(sender:)))
            
        }
        
        let count = self.tabBarController?.children.count ?? 1
        
        if count > 2 || self.children.count == 1 {
            viewController.hidesBottomBarWhenPushed = true
        }
        
        super.pushViewController(viewController, animated: true)
        
        
    }
    
    @objc func back(sender: UIButton){
        
        let _ = self.popViewController(animated: true)
        
    }
    
    override func popViewController(animated: Bool) -> UIViewController? {
        return super.popViewController(animated: animated)
    }

    ///解决tableview左滑删除手势和右滑返回手势的冲突
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        
        guard let view = touch.view else {
            return true
        }
        
        let str = NSStringFromClass(view.classForCoder)
        
        if str == "UITableViewCellContentView" || str == "UIScrollView"{
            return false
        }
        
        return true
        
    }
    
    /// 当前页面是否支持旋转，默认false
    override var shouldAutorotate: Bool{
        
        return false
        
    }
    
    // 返回当前显示的viewController支持旋转的方向
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
        
        return self.visibleViewController?.preferredInterfaceOrientationForPresentation ?? UIInterfaceOrientation.unknown
        
    }
    
}
